package com.jl.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jl.commons.constant.Constants;
import com.jl.commons.result.PageDataInfo;
import com.jl.mapper.MenuMapper;
import com.jl.mapper.RoleMenuMapper;
import com.jl.model.Menu;
import com.jl.model.RoleMenu;
import com.jl.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单资源表 服务实现类
 * </p>
 *
 * @author JiangJian
 * @since 2019-04-15
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;


    @Override
    public void selectMenuDataGrid(PageDataInfo<Map<String, Object>> pageDataInfo, QueryWrapper<Menu> qw) {
        Page<Map<String, Object>> page = new Page<>();
        page.setSize(pageDataInfo.getSize());
        page.setCurrent(pageDataInfo.getCurrent());
        List<Map<String, Object>> mapList = baseMapper.selectDataGrid(page, qw);
        pageDataInfo.setTotal(page.getTotal());
        pageDataInfo.setRecords(mapList);
    }

    @Override
    public boolean deleteMenuByIds(List<Long> ids) {
        //先删除role_menu中的数据
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("menu_id",ids);
        roleMenuMapper.delete(queryWrapper);
        //然后删除自身
        return baseMapper.deleteBatchIds(ids)>0;
    }

    @Override
    public List<Menu> selectNavMenuByRoleId(Long roleId) {
        return baseMapper.selectMenuListTypeByRoleId(roleId, Constants.MENU_TYPE_NAVIGATION);
    }

    @Override
    public List<Menu> selectMenuByRoleId(Long roleId) {
        return baseMapper.selectMenuListByRoleId(roleId);
    }

    @Override
    public List<Menu> selectMenuWithoutFunByRoleId(Long roleId) {
        return baseMapper.selectMenuWithoutTypeByRoleId(roleId, Constants.MENU_TYPE_FUNCTION);
    }

    @Override
    public List<Menu> selectMenuByRoleIds(List<Long> roleIds) {
        return baseMapper.selectMenuListByRoleIds(roleIds);
    }

    /**
     * 获取当前用户拥有的根菜单
     *
     * @param allMenus
     * @return
     */
    @Override
    public List<Menu> getRootMenuList(List<Menu> allMenus) {
        List<Menu> rootMenus = new ArrayList<Menu>();
        for (Menu menu : allMenus) {
            boolean isRootMenu = !rootMenus.contains(menu) && (menu.getParentId() == 0L || menu.getParentId() == null);
            if (isRootMenu) {
                rootMenus.add(menu);
            }
        }
        return rootMenus;
    }

    /**
     * 递归子节点
     *
     * @param menus    所有的菜单
     * @param parentId
     * @return
     */
    @Override
    public List<Menu> getchildMenus(List<Menu> menus, Long parentId) {
        List<Menu> childMenus = new ArrayList<Menu>();
        if (menus != null && !menus.isEmpty()) {
            Iterator<Menu> iterator = menus.iterator();
            while (iterator.hasNext()) {
                Menu menu = iterator.next();
                if (menu.getParentId().equals(parentId) && !childMenus.contains(menu)) {
                    childMenus.add(menu);
                    iterator.remove();
                }
            }
            // 依据orderby排序
            if (childMenus != null && !childMenus.isEmpty()) {
                for (Menu menu : childMenus) {
                    menu.setChildren(getchildMenus(menus, menu.getId()));
                }
            }
        }
        return childMenus;
    }
}
